﻿using FastCloud.Base.Desgin;
using FastCloud.ORG.Entity;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.ORG.DB
{
    public class DB_UserRole : BaseCacheML<Frame_UserRole>
    {
        public override void Insert(Frame_UserRole Entity)
        {
            if (string.IsNullOrEmpty(Entity.RelationGuid))
            {
                Entity.RelationGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
        }

        public List<Frame_UserRole> SelectAll()
        {
            return DTO.Select_Cache<Frame_UserRole>(); ;
        }

        public void UpdateUserRole(string UserGuid,string RoleGuid,bool IsCheck)
        {
            List<Frame_UserRole> UserRoleList = SelectAll().FindAll(a=>a.UserGuid==UserGuid && a.RoleGuid== RoleGuid);
            if (IsCheck)
            {
                if (UserRoleList.Count == 0)
                {
                    Frame_UserRole Entity = new Frame_UserRole();
                    Entity.UserGuid = UserGuid;
                    Entity.RoleGuid = RoleGuid;
                    Insert(Entity);
                }
            }
            else
            {
                if (UserRoleList.Count > 0)
                {
                    Frame_UserRole Entity = UserRoleList[0];
                    DeleteByID(Entity.RelationGuid);
                }
            }
        }
    }
}
